//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "bases.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TBaseForm *BaseForm;
//bool      editando;
bool      startup;
//---------------------------------------------------------------------------
__fastcall TBaseForm::TBaseForm(TComponent* Owner)
    : TForm(Owner)
{
    startup= true;
    BaseTrack->Position= MainForm->NumBase;
    BaseLabel->Caption= IntToStr(BaseTrack->Position)+"bits";
    startup= false;
    FormStyle= MainForm->FormStyle;
}
//---------------------------------------------------------------------------
void __fastcall TBaseForm::CreateParams(TCreateParams &Params)
{
    TForm::CreateParams(Params);  // call base class first
    Params.Style &= ~WS_CAPTION;  // then clear caption bit
}
//---------------------------------------------------------------------------
void __fastcall TBaseForm::SetVal(double valor)
{
    __int64    aux, mask, masked;
    union
    {
        float val;
        unsigned char c[4];
    } ff;

    union
    {
        double val;
        unsigned char c[8];
    } dd;

    aux= (__int64)valor;
    mask= 0;

    for(int i= 0; i < BaseTrack->Position; i++)
    {
        mask<<= 1;
        mask|= 1;
    }

    masked= aux&mask;

    if(((aux < 0) && (aux != ExtendSign(masked, BaseTrack->Position))) ||
       ((aux >= 0) && (aux != masked)))
    {
        HexEdit->Color= clRed;
        OctEdit->Color= clRed;
        BinEdit->Color= clRed;
    }
    else
    {
        HexEdit->Color= clYellow;
        OctEdit->Color= clYellow;
        BinEdit->Color= clYellow;
    }

    HexEdit->Text= Int64ToHex(masked);
    OctEdit->Text= Int64ToOct(masked);
    BinEdit->Text= Int64ToBin(masked);

    ff.val= (float)valor;//._M_re;
    FloatLabel->Caption= IntToHex(ff.c[3], 2)+"."+IntToHex(ff.c[2], 2)+"."+
                         IntToHex(ff.c[1], 2)+"."+IntToHex(ff.c[0], 2);

    dd.val= valor;//._M_re;
    DoubleLabel->Caption= IntToHex(dd.c[7], 2)+"."+IntToHex(dd.c[6], 2)+"."+
                          IntToHex(dd.c[5], 2)+"."+IntToHex(dd.c[4], 2)+"."+
                          IntToHex(dd.c[3], 2)+"."+IntToHex(dd.c[2], 2)+"."+
                          IntToHex(dd.c[1], 2)+"."+IntToHex(dd.c[0], 2);
}
//---------------------------------------------------------------------------
AnsiString __fastcall TBaseForm::Int64ToHex(__int64 valor)
{
    AnsiString  tmp;
    const char  digit[]= "0123456789ABCDEF";

    do
    {
        tmp.Insert((AnsiString)digit[(char)valor&0xf], 0);
        valor>>= 4;
        valor&= 0x0fffffffffffffff;
    }
    while(valor);

    return tmp;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TBaseForm::Int64ToOct(__int64 valor)
{
    AnsiString tmp;

    do
    {
        tmp.Insert((AnsiString)(short)(valor&0x7), 0);
        valor>>= 3;
        valor&= 0x0fffffffffffffff;
    }
    while(valor);

    return tmp;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TBaseForm::Int64ToBin(__int64 valor)
{
    AnsiString tmp;

    do
    {
        tmp.Insert((AnsiString)(short)(valor&0x1), 0);
        valor>>= 1;
        valor&= 0x0fffffffffffffff;
    }
    while(valor);

    return tmp;
}
//---------------------------------------------------------------------------

void __fastcall TBaseForm::FormActivate(TObject *Sender)
{
    Color= (TColor)0xff8484;
    MainForm->Editando= false;
    MainForm->Decimal= 0;
    MainForm->EditLine= "";
//    editando= true;
    MainForm->InfoUpdate();
}
//---------------------------------------------------------------------------

void __fastcall TBaseForm::FormDeactivate(TObject *Sender)
{
    Color= (TColor)0xc1ffff;
}
//---------------------------------------------------------------------------

void __fastcall TBaseForm::FormHide(TObject *Sender)
{
    MainForm->BaseBtn->Down= false;
}
//---------------------------------------------------------------------------

void __fastcall TBaseForm::FormKeyPress(TObject *Sender, char &Key)
{
    if(Key == 13)
    {
        MainForm->SetFocus();

        if((TEdit*)ActiveControl->Tag)
        {
            for(int i= 31; i; MainForm->val[i]= MainForm->val[i-1], i--);

            MainForm->val[0]= ExtendSign(ToDecimal(
                                              ((TEdit*)ActiveControl)->Text,
                                              ((TEdit*)ActiveControl)->Tag),
                                          BaseTrack->Position);
            MainForm->InfoUpdate();
        }
        return;
    }

    if(Key == '\b')
        return;

    Key= (char)((Key >= 'a' && Key <= 'z')? Key&0x5f : Key);

    if(ActiveControl == HexEdit &&
       !( ((Key >= 0x30 && Key <= 0x39) ||
           (Key >= 'A' && Key <= 'F')) ))
        Key= 0;

    if(ActiveControl == OctEdit &&
       !(Key >= 0x30 && Key <= 0x37))
        Key= 0;

    if(ActiveControl == BinEdit &&
       !(Key == 0x30 || Key == 0x31))
        Key= 0;
}
//---------------------------------------------------------------------------

void __fastcall TBaseForm::FullSel(TObject *Sender)
{
    TEdit *edit= dynamic_cast < TEdit *> (Sender);

    if(edit)
        edit->Clear();
}
//---------------------------------------------------------------------------

__int64 __fastcall TBaseForm::ToDecimal(AnsiString t, int base)
{
    __int64 tmp= 0;

    for(int i= 0; i < t.Length(); i++)
    {
        tmp*= base;
        if(t[i+1] >= '0' && t[i+1] <= '9')
            tmp+= t[i+1]-0x30;
        else
            tmp+= t[i+1]-'A'+10;
    }

    return tmp;
}
//---------------------------------------------------------------------------

__int64 __fastcall TBaseForm::ExtendSign(__int64 val, int base)
{
    __int64 tmp= val;
    __int64 mask= 1<<(base-1);
    bool    s= val&mask;

    for(int i= base; i < 64; i++)
    {
        mask<<= 1;
        if(s)
            tmp|= mask;
        else
            tmp&= ~mask;
    }

    return tmp;
}
//---------------------------------------------------------------------------

void __fastcall TBaseForm::BaseTrackChange(TObject *Sender)
{
    BaseLabel->Caption= IntToStr(BaseTrack->Position)+"bits";
    MainForm->NumBase= BaseTrack->Position;
    MainForm->InfoUpdate();

    if(!startup)
        MainForm->SetFocus();
}
//---------------------------------------------------------------------------

